<?php

namespace App\Services\Chanjet;

//格式化记录日志
use App\Models\Chanjet\TplusRequestError;
use Illuminate\Support\Facades\Log;

Class LogFormatRecord {
    public static function record($url='',$type='post',$arguments =[],$response = '')
    {
        $log_con = '';
        if(!empty($arguments))
        {
            $log_con = $url . ' ' . $type .json_encode($arguments);
        }
        if(!empty($response))
        {
            $log_con = $log_con . " ".json_encode($response);
        }
        $log = $log_con;
        //Log::info($log);
        Log::channel('tplus')->info($log);
    }
    //错误收集
    public static function error($data = []){
        /**
         *   $requestData = [
         *      "api" => "partnerCreate",
         *      "param" => $custom,
         *      "mangerinfo" => $this->mangerinfo
         *   ];
         *
        */
        //$field = ["cpid","org_id","business_type","api","error","param","res"];
        $assembledData = [
            "cpid" => $data["mangerinfo"]["cpid"],
            "org_id" => $data["mangerinfo"]["org_id"],
            "manger_id" => $data["mangerinfo"]["id"],
            "api" => $data["api"],
            "param" => json_encode($data["param"],true),
            "error" => $data["error"],
            "create_at" => time()
        ];
        if(!empty($data)){
//            $insertData = $assembledData;
//            foreach($assembledData as $item){
//                if(isset($data[$item])){
//                    $insertData[$item] = $data[$item];
//                }
//            }
            //$insertData["create_at"] = time();
            TplusRequestError::create($assembledData);
        }
    }
}
